Skip to content

fix(typescript): drain queued events before ending the stream iterator - #44

Open
OsamaAnsar wants to merge 1 commit into
xdevplatform:mainfrom
OsamaAnsar:fix/typescript-stream-drain-queue
Open

OsamaAnsar wants to merge 1 commit into
xdevplatform:mainfrom
OsamaAnsar:fix/typescript-stream-drain-queue

Conversation

@OsamaAnsar

Copy link
Copy Markdown

Problem

EventDrivenStream's async iterator (xdk-gen/templates/typescript/event_driven_stream.j2) loops on while (!isComplete && !hasError). As soon as the close or error event sets a flag, the loop exits, so anything already sitting in dataQueue is thrown away.

Repro: the server sends three posts in one chunk and ends the response.

server sent ids: 1,2,3
iterator yielded ids: []

With the stream left open (control), the same iterator yields all three (["1","2","3"]). The same loss happens when the connection errors right after data arrives: the queued events are dropped and the error is thrown immediately.

Fix

Keep looping while the queue is non-empty and only stop once it is drained:

while (dataQueue.length > 0 || (!isComplete && !hasError)) {

The recorded error (if any) is still thrown, but only after the queue is empty. Behavior for a stream that is still open is unchanged.

Tests

New test template test_event_driven_stream.j2 (registered in generator.rs next to test_client_errors) writes tests/event_driven_stream.test.ts, covering:

  • 3 posts then close: all 3 are yielded
  • 3 posts then a stream error: all 3 are yielded, then the error is thrown

How this was verified

I do not have a Rust toolchain, so I did not run the generator or cargo test / cargo fmt / cargo clippy; CI will regenerate the SDK. Instead I applied the identical one-line change by hand to a copy of the current xdk-typescript output (v0.6.6) and ran the new test file there with npm ci + jest:

  • before the fix: 2 failed, 2 total (iterator yields [])
  • after the fix: 2 passed, 2 total (stable over repeated runs)
  • full jest suite on the patched copy: 77 suites / 1155 tests passing, tsc --noEmit clean

The only Rust change is one added render line in the tests: list.

The async iterator on EventDrivenStream exited its loop as soon as the
close or error flag was set, discarding any events that were already in
its queue. A server that sends a few posts and then ends the response
(or a connection that fails right after data arrived) made
`for await (const event of stream)` yield nothing.

Keep looping while the queue is non-empty, and only finish (or throw the
recorded error) once it has been drained. Adds a jest test template
covering the close and error cases.
@CLAassistant

CLAassistant commented Oct 21, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants